const _ = require('lodash');
const soag = require('../modules/lilly-soag');

module.exports = (SystemUser) => {
  const userModel = SystemUser;
  userModel.findUserByEmployeeId = (employeeId, callback) => {
    soag.getUserProfile(employeeId)
        .then((result) => {
          callback(null, result.data[0]);
        })
        .catch(callback);
  };
  userModel.remoteMethod('findUserByEmployeeId', {
    accepts: { arg: 'query', type: 'object' },
    returns: { arg: 'user', type: 'array' },
  });
  userModel.findUserListByQuery = (query, callback) => {
    const queryUserList = [];
    soag.getUserByQuery(query)
        .then((result) => {
          _.forEach(result.data, (data) => {
            if (data.STTS_IND == 1) {
              const queryUseItem = soag.parseUser(data);
              queryUserList.push(queryUseItem);
            }
          });
          callback(null, queryUserList);
        })
        .catch(callback);
  };
  userModel.remoteMethod('findUserListByQuery', {
    accepts: { arg: 'query', type: 'object' },
    returns: { arg: 'user', type: 'array' },
  });
  userModel.findHierarchyByEmployeeId = (employeeId, yearMonthId, callback) => {
    if (_.isEmpty(callback)) {
      callback = yearMonthId;
      yearMonthId = null;
    }
    soag.getHierarchyByEmployeeID(employeeId, yearMonthId)
        .then((result) => {
          callback(null, result.data[0]);
        })
        .catch(callback);
  };
  userModel.remoteMethod('findHierarchyByEmployeeId', {
    accepts: [
      { arg: 'employeeId', type: 'string' },
      { arg: 'yearMonthId', type: 'number' },
    ],
    returns: { arg: 'user', type: 'object' },
  });
};
